6 class HTMLMultiSelectField
extends HTMLFormField
implements HTMLNestedFilterable
{
9 * In adition to the usual HTMLFormField parameters, this can take the following fields:
10 * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
11 * can be used to filter them. This is desirable mostly for very long lists of options.
12 * This only works for users with JavaScript support and falls back to the list of checkboxes.
13 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
14 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
15 * for very short lists of concisely labelled options.
17 public function __construct( $params ) {
18 parent
::__construct( $params );
20 // If the disabled-options parameter is not provided, use an empty array
21 if ( isset( $this->mParams
['disabled-options'] ) === false ) {
22 $this->mParams
['disabled-options'] = [];
25 // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
26 if ( isset( $params['dropdown'] ) ||
strpos( $this->mClass
, 'mw-chosen' ) !== false ) {
27 $this->mClass
.= ' mw-htmlform-dropdown';
30 if ( isset( $params['flatlist'] ) ) {
31 $this->mClass
.= ' mw-htmlform-flatlist';
35 public function validate( $value, $alldata ) {
36 $p = parent
::validate( $value, $alldata );
42 if ( !is_array( $value ) ) {
46 # If all options are valid, array_intersect of the valid options
47 # and the provided options will return the provided options.
48 $validOptions = HTMLFormField
::flattenOptions( $this->getOptions() );
50 $validValues = array_intersect( $value, $validOptions );
51 if ( count( $validValues ) == count( $value ) ) {
54 return $this->msg( 'htmlform-select-badoption' );
58 public function getInputHTML( $value ) {
59 if ( isset( $this->mParams
['dropdown'] ) ) {
60 $this->mParent
->getOutput()->addModules( 'jquery.chosen' );
63 $value = HTMLFormField
::forceToStringRecursive( $value );
64 $html = $this->formatOptions( $this->getOptions(), $value );
69 public function formatOptions( $options, $value ) {
72 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
74 foreach ( $options as $label => $info ) {
75 if ( is_array( $info ) ) {
76 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
77 $html .= $this->formatOptions( $info, $value );
80 'id' => "{$this->mID}-$info",
83 if ( in_array( $info, $this->mParams
['disabled-options'], true ) ) {
84 $thisAttribs['disabled'] = 'disabled';
86 $checked = in_array( $info, $value, true );
88 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs, $label );
90 $html .= ' ' . Html
::rawElement(
92 [ 'class' => 'mw-htmlform-flatlist-item' ],
101 protected function getOneCheckbox( $checked, $attribs, $label ) {
102 if ( $this->mParent
instanceof OOUIHTMLForm
) {
103 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
105 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
107 Xml
::check( "{$this->mName}[]", $checked, $attribs ) .
109 call_user_func( $elementFunc,
111 [ 'for' => $attribs['id'] ],
114 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
115 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
117 Html
::closeElement( 'div' );
124 * Get options and make them into arrays suitable for OOUI.
125 * @return array Options for inclusion in a select or whatever.
127 public function getOptionsOOUI() {
128 $options = parent
::getOptionsOOUI();
129 foreach ( $options as &$option ) {
130 $option['disabled'] = in_array( $option['data'], $this->mParams
['disabled-options'], true );
136 * Get the OOUI version of this field.
139 * @param string[] $value
140 * @return OOUI\CheckboxMultiselectInputWidget
142 public function getInputOOUI( $value ) {
143 $this->mParent
->getOutput()->addModules( 'oojs-ui-widgets' );
146 $attr['id'] = $this->mID
;
147 $attr['name'] = "{$this->mName}[]";
149 $attr['value'] = $value;
150 $attr['options'] = $this->getOptionsOOUI();
152 if ( $this->mOptionsLabelsNotFromMessage
) {
153 foreach ( $attr['options'] as &$option ) {
154 $option['label'] = new OOUI\
HtmlSnippet( $option['label'] );
158 $attr +
= OOUI\Element
::configFromHtmlAttributes(
159 $this->getAttributes( [ 'disabled', 'tabindex' ] )
162 if ( $this->mClass
!== '' ) {
163 $attr['classes'] = [ $this->mClass
];
166 return new OOUI\
CheckboxMultiselectInputWidget( $attr );
170 * @param WebRequest $request
172 * @return string|array
174 public function loadDataFromRequest( $request ) {
175 if ( $this->isSubmitAttempt( $request ) ) {
176 // Checkboxes are just not added to the request arrays if they're not checked,
177 // so it's perfectly possible for there not to be an entry at all
178 return $request->getArray( $this->mName
, [] );
180 // That's ok, the user has not yet submitted the form, so show the defaults
181 return $this->getDefault();
185 public function getDefault() {
186 if ( isset( $this->mDefault
) ) {
187 return $this->mDefault
;
193 public function filterDataForSubmit( $data ) {
194 $data = HTMLFormField
::forceToStringRecursive( $data );
195 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
198 foreach ( $options as $opt ) {
199 $res["$opt"] = in_array( $opt, $data, true );
205 protected function needsLabel() {